#region Using

using System;
using System.Collections.Generic;

#endregion

namespace BlogEngine.Core
{
	/// <summary>
	/// A link is a reference to some form URL - normally used for the BlogRoll.
	/// </summary>
	[Serializable]
	public class Link : BlogBase<Link>, IComparable<Link>
	{

		#region Constructor

		/// <summary>
		/// The default constructor assigns default values.
		/// </summary>
		public Link() :base()
		{
        }

		#endregion

		#region Properties

		private string _Website = string.Empty;
		/// <summary>
		/// Gets or sets the Website Uri.
		/// </summary>
		public string Website
		{
			get { return _Website; }
			set
			{
				if (_Website != value) MarkChanged("Website");
				_Website = value;
			}
		}

		private string _Feed = string.Empty;
		/// <summary>
		/// Gets or sets the Feed Uri.
		/// </summary>
		public string Feed
		{
			get { return _Feed; }
			set
			{
				if (_Feed != value) MarkChanged("Feed");
				_Feed = value;
			}
		}

        private string _XFNTag = string.Empty;
        /// <summary>
        /// Gets or sets the XFN Relationship Tag information
        /// </summary>
        public string XFNTag
        {
            get { return _XFNTag; }
            set 
            {
                if (_XFNTag != value) MarkChanged("XFNTag");
                _XFNTag = value;
            }
        }

		private string _Image = string.Empty;
		/// <summary>
		/// Gets or sets the Image Uri.
		/// </summary>
		public string Image
		{
			get { return _Image; }
			set
			{
				if (_Image != value) MarkChanged("Image");
				_Image = value;
			}
		}

        private bool _BlogRoll;

        public bool BlogRoll
        {
            get { return _BlogRoll; }
            set
            {
                if (_BlogRoll != value) MarkChanged("BlogRoll");
                _BlogRoll = value;
            }
        }


		#endregion

		#region Methods

        private List<Category> _Categories = new List<Category>();
        /// <summary>
        /// An unsorted List of categories.
        /// </summary>
        public List<Category> Categories
        {
            get { return _Categories; }
        }


        private bool _IsPublished;

        /// <summary>
        /// Gets or sets the IsPublished or the object.
        /// </summary>
        public bool IsPublished
        {
            get { return _IsPublished; }
            set
            {
                if (_IsPublished != value) MarkChanged("IsPublished");
                _IsPublished = value;
            }
        }

        /// <summary>
        /// Returns a collection of Link records for the Category provided
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public static List<Link> GetByCategory(Guid categoryId)
        {
            Category cat = Category.GetById(categoryId);

            List<Link> list = new List<Link>();
            foreach (Link item in Link.Items)
            {
                if (item.Categories.Contains(cat))
                {
                    list.Add(item);
                }
            }

            list.Sort();
            return list;
        }

		#endregion

		#region IComparable<Link> Members

		/// <summary>
		/// Compares the current object with another object of the same type.
		/// </summary>
		/// <param name="other">An object to compare with this object.</param>
		/// <returns>
		/// A 32-bit signed integer that indicates the relative order of the 
		/// objects being compared. The return value has the following meanings: 
		/// Value Meaning Less than zero This object is less than the other parameter.Zero 
		/// This object is equal to other. Greater than zero This object is greater than other.
		/// </returns>
		public int CompareTo(Link other)
		{
            return CompareOrderAndTitle(this.Order, DateTime.MinValue, this.Title, other.Order, DateTime.MinValue, other.Title);
		}

		#endregion

		#region Data Access

		static internal BlogEngine.Core.Provider.Interfaces.ILinks LinkProvider = BlogEngine.Core.Provider.BlogService.Provider.GetInterface<BlogEngine.Core.Provider.Interfaces.ILinks>();
		
		protected override void DataDelete()
		{
			LinkProvider.Delete(this);
		}

		protected override void DataInsert()
		{
			LinkProvider.Insert(this);
		}

		protected override Link DataSelect(Guid id)
		{
			return LinkProvider.Select(id);
		}

		protected override void DataUpdate()
		{
			LinkProvider.Update(this);
		}

		private static int Dummy = OverrideOnLoad();

		private static int OverrideOnLoad()
		{
			OnLoad = LinkProvider.Fill;
			return 0;
		}

		#endregion
    }
}
